package com.pronhub.cloud.shop.redis;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration//说明了这个是一个配置类
@EnableConfigurationProperties(RedisProperties.class)//创建RedisProperties这个类 从容器中获取
//onXXX必须满足这个注解才能生效 也就是在码云上能拉下来相对应的配置
@ConditionalOnProperty(prefix = "redis",value = "host")//在配置文件中需要配置Redis.host这个属性 当前配置类才生效,否则不生效
public class RedisAutoConfiguration {

    @Bean
    public JedisPool jedisPool(RedisProperties redisProperties){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(redisProperties.getPoolMaxTotal());//最大连接数
        config.setMaxIdle(redisProperties.getPoolMaxIdle());//最大连接数
        config.setMaxWaitMillis(redisProperties.getPoolMaxWait()*1000);
        return new JedisPool(config,redisProperties.getHost(),
                redisProperties.getPort(),redisProperties.getTimeout(),redisProperties.getPassword());
    }

    @Bean
    public MyRedisTemplate myRedisTemplate(){
        return new MyRedisTemplate();
    }
}
